package com.example.board.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.board.pojo.User;
import com.example.board.pojo.vo.ChartDataData;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

@Repository
public interface SalesPlanMapper extends BaseMapper<User> {

    @Select({
            "<script>",
            "SELECT u.id id,u.nickname nickname , u.avatar avatar from sys_user u,sd_tool_man_info sd ",
            "<where> 1 = 1 and u.id = sd.user_id and sd.deleted_at is null and u.enabled = 1 ",

            "<if test='city != null and city != \"\" '> ",
            "and sd.city = #{city} ",
            "</if> ",


            "<if test='province != null and province != \"\" '> ",
            "and sd.province = #{province} ",
            "</if> ",
            "</where> ",
            "GROUP BY  u.id,u.nickname",
            "</script>",
    })
    List<User> findSysUser(@Param("province") String province,
                           @Param("city") String city);

    @Select({
            "<script>",
            "SELECT count(sd.id) total , u.id id ,u.nickname name, sd.created_at timeLocal  from sys_user u,sd_user_record sd ",
            "where  u.enabled = 1 and sd.tool_man_id = u.id ",
            "and u.id in  ",
            " <foreach collection=\"ids\" item=\"userId\" index=\"index\" open=\"(\" separator=\",\" close=\")\">",
            "#{userId}",
            "</foreach>",
            " and sd.created_at BETWEEN #{start} and #{end} ",
            "GROUP BY  u.id,u.nickname , sd.created_at",
            "</script>",
    })
    List<ChartDataData> findTotalUserRe(@Param("ids")List<Long> collect,
                                        @Param("start") LocalDateTime start,
                                        @Param("end") LocalDateTime end);

    @Select({
            "<script>",
            "SELECT count(sd.id) total, u.id id,u.nickname name, sd.created_at  timeLocal from sys_user u,sd_visit_user_record sd ",
            "where  u.enabled = 1 and sd.tool_man_id = u.id ",
            "and u.id in  ",
            " <foreach collection=\"ids\" item=\"userId\" index=\"index\" open=\"(\" separator=\",\" close=\")\">",
            "#{userId}",
            "</foreach>",
            " and sd.created_at BETWEEN #{start} and #{end} ",
            "GROUP BY  u.id,u.nickname , sd.created_at",
            "</script>",
    })
    List<ChartDataData> findTotalVisity(@Param("ids")List<Long> collect,
                                        @Param("start") LocalDateTime start,
                                        @Param("end") LocalDateTime end);

    @Select({
            "<script>",
            "SELECT count(o.id) total , u.id id,u.nickname name, o.order_time timeLocal  from sys_user u,sd_order o ",
            "where  u.enabled = 1 and o.tool_man_id = u.id ",
            "and u.id in  ",
            " <foreach collection=\"ids\" item=\"userId\" index=\"index\" open=\"(\" separator=\",\" close=\")\">",
            "#{userId}",
            "</foreach>",
            " and o.order_time BETWEEN #{start} and #{end} and o.on_cancel = 0 ",
            " and o.on_custom_product_image = 1 ",
            "GROUP BY  u.id,u.nickname , o.order_time ",
            "</script>",
    })
    List<ChartDataData> findCustomized(@Param("ids")List<Long> collect,
                                       @Param("start") LocalDateTime start,
                                       @Param("end") LocalDateTime end);

    @Select({
            "<script>",
            "SELECT count(o.id) total, u.id id,u.nickname name, o.order_time timeLocal from sys_user u,sd_order o ",
            "where  u.enabled = 1 and o.tool_man_id = u.id ",
            "and u.id in  ",
            " <foreach collection=\"ids\" item=\"userId\" index=\"index\" open=\"(\" separator=\",\" close=\")\">",
            "#{userId}",
            "</foreach>",
            " and o.order_time BETWEEN #{start} and #{end} and o.on_cancel = 0 ",
            " and o.on_custom_product_image = 0 ",
            "GROUP BY  u.id,u.nickname , o.order_time ",
            "</script>",
    })
    List<ChartDataData> findStandard(@Param("ids")List<Long> collect,
                                     @Param("start") LocalDateTime start,
                                     @Param("end") LocalDateTime end);



    @Select({
            "<script>",
            "SELECT u.id id,u.nickname nickname , d.name deptName , u.dept_id deptId from sys_user u,sd_tool_man_info sd , sys_dept d ",
            "<where> 1 = 1 and u.id = sd.user_id and sd.deleted_at is null and u.enabled = 1 ",
            " and d.id = u.dept_id ",
            "<if test='city != null and city != \"\" '> ",
            "and sd.city = #{city} ",
            "</if> ",


            "<if test='province != null and province != \"\" '> ",
            "and sd.province = #{province} ",
            "</if> ",

            "<if test='deptName != null and deptName != \"\" '> ",
            "and d.name = #{deptName} ",
            "</if> ",

            "</where> ",
            "GROUP BY  u.id,u.nickname",
            "</script>",
    })
    List<User> findSysUserDept(@Param("province") String province,
                               @Param("city") String city,
                               @Param("deptName") String deptName);


    @Select({
            "<script>",
            "SELECT u.id id,u.nickname nickname , u.avatar avatar from sys_user u,sd_tool_man_info sd ",
            "<where> 1 = 1 and u.id = sd.user_id and sd.deleted_at is null and u.enabled = 1 ",
            " and  u.id in  ",
            " <foreach collection=\"ids\" item=\"userId\" index=\"index\" open=\"(\" separator=\",\" close=\")\">",
            "#{userId}",
            "</foreach>",

            "</where> ",
            "GROUP BY  u.id,u.nickname",
            "</script>",
    })
    List<User> findById(@Param("ids")List<Long> collect);
}
